package ui;


import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Message;
import model.MessageType;

import com.ibm.icu.text.DateFormat;

public class CalendarPanel extends JPanel{

	public CalendarPanel(){
		
		

		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[]{100, 100, 100};
		gbl.rowHeights = new int[]{10,10,100};
		gbl.columnWeights = new double[]{1.0, 0.0,1.0};
		gbl.rowWeights = new double[]{0.0,0.0,1.0};

		this.setLayout(gbl);
		

		GridBagConstraints gbc_left = new GridBagConstraints();

		gbc_left.anchor = GridBagConstraints.NORTHWEST;
		gbc_left.gridx = 0;
		gbc_left.gridy = 2;
		gbc_left.fill = GridBagConstraints.BOTH;
		gbc_left.insets = new Insets(0,0,0,0);
		this.add(this.leftPanel(), gbc_left);
		
		GridBagConstraints gbc_center = new GridBagConstraints();

		gbc_center.anchor = GridBagConstraints.NORTH;
		gbc_center.gridx = 1;
		gbc_center.gridy = 2;
		gbc_center.insets = new Insets(0,0,0,0);
		gbc_left.fill = GridBagConstraints.VERTICAL;
		this.add(this.centerPanel(), gbc_center);
		
		GridBagConstraints gbc_right = new GridBagConstraints();
		gbc_right.anchor = GridBagConstraints.NORTHEAST;
		gbc_right.gridx = 2;
		gbc_right.gridy = 2;
		gbc_right.insets = new Insets(0,0,0,0);
		gbc_right.fill = GridBagConstraints.BOTH;
		this.add(this.rightPanel(), gbc_right);

		GridBagConstraints gbc_rightTitle = new GridBagConstraints();
		gbc_rightTitle.anchor = GridBagConstraints.NORTHEAST;
		gbc_rightTitle.gridx = 2;
		gbc_rightTitle.gridy = 1;
		gbc_rightTitle.insets = new Insets(0,0,0,0);
		gbc_rightTitle.fill = GridBagConstraints.HORIZONTAL;
		this.add(this.rightTitle(), gbc_rightTitle);
		
		
		GridBagConstraints gbc_centerTitle = new GridBagConstraints();
		gbc_centerTitle.anchor = GridBagConstraints.NORTHEAST;
		gbc_centerTitle.gridx = 1;
		gbc_centerTitle.gridy = 1;
		gbc_centerTitle.insets = new Insets(0,0,0,0);
		gbc_centerTitle.fill = GridBagConstraints.HORIZONTAL;
		this.add(this.centerTitle(), gbc_centerTitle);

		GridBagConstraints gbc_message = new GridBagConstraints();
		gbc_message.anchor = GridBagConstraints.NORTHEAST;
		gbc_message.gridx = 0;
		gbc_message.gridy = 0;
		gbc_message.insets = new Insets(0,0,0,0);
		gbc_message.fill = GridBagConstraints.BOTH;
		gbc_message.gridwidth = 3;
		
		Message m = new Message(new Date(),"Apartment #3: Washing mashine", "Washing mashing is broken", MessageType.COMPLAINT);		
		this.add(this.message(m), gbc_message);
		validate();
		repaint();
	}
	
	private JPanel leftPanel()
	{

		JPanel p = new MessageTaskPanel("Message Tasks",50, new Date());
		
//		p.setBackground(Color.YELLOW);
//		p.add(new MessageTaskPanel("Message Tasks",50, new Date()));
		return p;
	}
	
	private JPanel rightPanel()
	{
		JPanel p=new CalendarTaskPanel("Todays Tasks",100,new Date());
//		JPanel p = new JPanel();
//		p.setBackground(Color.RED);
//		p.add(new CalendarTaskPanel("Todays Tasks",100,new Date()));
		return p;
	}
	
	private JPanel centerPanel()
	{
		JPanel p = new DatePicker();//new JPanel();
		p.setBackground(Color.ORANGE);
		//p.setPreferredSize(new Dimension(300,300));

		return p;
	}
	private JPanel leftTitle()
	{

//		JPanel p = new JPanel();
//		p.setBackground(Color.YELLOW);
//		p.add(new MessageTaskPanel("Message Tasks",50));
//		return p;
		return null;
	}
	
	private JPanel rightTitle()
	{
		Font font = new Font("Segoe UI Black", Font.BOLD,11);
		JPanel p=new JPanel();
		JLabel label2 = new JLabel("Tasks for "+DateFormat.getDateInstance(DateFormat.SHORT,Locale.getDefault()).format(new Date()));
		label2.setFont(font);
		p.add(label2);
		return p;
	}
	
	private JPanel centerTitle()
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
		int year = cal.get(Calendar.YEAR);
		Font font = new Font("Segoe UI Black", Font.BOLD,11);
		//String text = month+" "+year;
		JPanel p=new JPanel();
		JLabel label2 = new JLabel(month+" "+year);
		label2.setFont(font);
		p.add(label2);
		return p;
		//return null;
	}
	
	private JPanel message(Message m)
	{
		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		Color bgColor = Color.ORANGE;
		String sender = "";
		
		switch(m.getType())
		{
			case SYSTEM:
				bgColor= Color.LIGHT_GRAY;
				sender = "System";
				break;
			case UNSOLVEDTASK:
				bgColor= Color.LIGHT_GRAY;
				sender = "System";
				break;
			case EMPTYWAITINGLIST:
				bgColor= Color.LIGHT_GRAY;
				sender = "System";
				break;
			case APPLICATION:
				bgColor= Color.GREEN;
				sender = "New renter";
				break;
			case CUSTOM:
				bgColor = Color.YELLOW;
				sender = "User";
				break;
			case COMPLAINT: 
				bgColor= Color.RED;
				sender = "Renter";
				break;
			case UNDEFINED:
				sender = "Unknown";
				break;
		}
		
		Font font = new Font("Segoe UI Black", Font.BOLD,20);
		Font font2 = new Font("Segoe UI Black", Font.BOLD,12);
		
		JLabel label1 = new JLabel(m.getTitle());
		label1.setFont(font);
		
		JLabel label2 = new JLabel("<html>"+m.getComment()+"<br>Created on: "+m.getCreated()+"<br>Message sent by: "+sender+"</html>");
		label2.setFont(font2);
		p.setBackground(bgColor);
		p.add(label1,BorderLayout.PAGE_START);
        p.add(label2,BorderLayout.CENTER);
        
        

		return p;
	}
}